package RP2;

import java.lang.reflect.Field;

public class MyObjectFactory {

	public static void main(String[] args) {
		getObject("RP2.Person");
		getObject("RP2.Dog");
		getObject("RP2.Book");
		
	}
	
	public static void getObject(String className) {
		Class<?> clazz;
		try {
			clazz = Class.forName(className);
			Object o = clazz.newInstance();
			Field[] fields = clazz.getFields();
			for(Field f : fields) {
				if(f.getName().equals("name")) {
					f.set(o, "name initial value");
					System.out.println("name:" + f.get(o));
				}
				if(f.getName().equals("age")) {
					f.set(o, 1);
					System.out.println("age:" + f.get(o));
				}
			}
			Field f1 = clazz.getField("name");
			f1.set(o, "lisi");
			f1.get(o);
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
			e.printStackTrace();
		}
		
	}

}
